package student.action;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import edu.student.models.Announcements;
import edu.student.models.News;
import edu.student.models.User;



import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;



public class Grades extends ActionSupport{
	 /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<String> semesterlist=new ArrayList<String>();
	private ArrayList<String> subjectlist=new ArrayList<String>();
	private String roll_no;
	
	private String username="";
	private List<News> news;
	private List<Announcements> announcements;
	private String image="";
	private Timestamp lastLogin;
	private String userid="";
	private float cgpa;
	
	
	public String execute() throws Exception {
		User user=new User();
		Map<String, Object> session = ActionContext.getContext().getSession();
		user = (User) session.get("user");
		
		roll_no=user.getRollNor();
		userid=user.getRollNor();
		username=user.getUsername();
		image=user.getImage();
		lastLogin=user.getLastLogin();
		news = News.getNews(" where ui.roll_No='"+userid+"' ");
		announcements=Announcements.getAnnouncements(" where ui.roll_No='"+userid+"' ");
		System.out.println("\nroll_no is "+roll_no);
		 semesterlist=(ArrayList<String>)findSemester(roll_no);
		 //semesterlist.add("none");
		 setCgpa(findCGPA(roll_no));
		 
		
		return "initial";
		
		//return "success";
	}
	public static ArrayList<String> findSemester(String roll_no){
		
			ArrayList<String> selection = new ArrayList<String>();
			ResultSet result = null;
			Connection con = DB.getConnection();
			System.out.println("roll_no inside findSemester = " + roll_no);
			String query = "SELECT DISTINCT SEMESTER.SEMESTER_NAME FROM SEMESTER,ENROLLMENT  "
			+ "WHERE ENROLLMENT.SEMESTER_ID =SEMESTER.SEMESTER_ID AND ENROLLMENT.ROLL_NO = '" + roll_no + "' AND ENROLLMENT.IS_ENROLLED = 'Y' ";
			result = DB.readFromDB(query, con);
			try {
			while (result.next()) {
			String sem;
			sem=result.getString("SEMESTER.SEMESTER_NAME");
			System.out.println("semester is" + sem);
			selection.add(sem);
			}
			} catch (SQLException e) {
			      System.out.println("Exception while reading from db"+ e);
			}
			DB.close(con);
			
			return selection;
			}
	public static float findCGPA(String roll_no){
		
		float gradePoint=0;
		float cgpa=0;
		int count=0;
		ResultSet result = null;
		Connection con = DB.getConnection();
		String query ="SELECT GRADE_POINT FROM ENROLLMENT WHERE ROLL_NO = '" + roll_no + "' AND ENROLLMENT.IS_ENROLLED = 'Y'  ";
		result = DB.readFromDB(query, con);
		try{
			while(result.next()){
				gradePoint=gradePoint + result.getFloat("GRADE_POINT");
				count = count + 1;
			}
			cgpa= gradePoint/count;
		}catch (SQLException e) {
		      System.out.println("Exception while reading from db"+ e);
		}
		DB.close(con);
		
		return cgpa;
	}
	
	public ArrayList<String> getSemesterlist() {
		return semesterlist;
	}
	public void setSemesterlist(ArrayList<String> semesterlist) {
		this.semesterlist = semesterlist;
	}
	public String getRoll_no() {
		return roll_no;
	}
	public void setRoll_no(String roll_no) {
		this.roll_no = roll_no;
	}
	public ArrayList<String> getSubjectlist() {
		return subjectlist;
	}
	public void setSubjectlist(ArrayList<String> subjectlist) {
		this.subjectlist = subjectlist;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public List<News> getNews() {
		return news;
	}
	public void setNews(List<News> news) {
		this.news = news;
	}
	public List<Announcements> getAnnouncements() {
		return announcements;
	}
	public void setAnnouncements(List<Announcements> announcements) {
		this.announcements = announcements;
	}
	public String getImage() {
		return image;
	}
	public void setImage(String image) {
		this.image = image;
	}
	public Timestamp getLastLogin() {
		return lastLogin;
	}
	public void setLastLogin(Timestamp lastLogin) {
		this.lastLogin = lastLogin;
	}
	public String getUserid() {
		return userid;
	}
	public void setUserid(String userid) {
		this.userid = userid;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public float getCgpa() {
		return cgpa;
	}
	public void setCgpa(float cgpa) {
		this.cgpa = cgpa;
	}
	
	
	
}
